package com.jinyou.utils.common;

import java.math.BigDecimal;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SysCommon {

    public static boolean hasKeyword(String target, String keyword) {
        if (ValidateUtil.isNotNull(target) && target.contains(keyword)) {
            return true;
        }
        // 例如搜索的y 既能搜索到 呀 也能 搜索到 一
        if (ValidateUtil.isNotNull(target) && ValidateUtil.isNotNull(keyword)) {
            target = JYPinyinHelper.toHanyuPinyin(target);
            if (ValidateUtil.isNotNull(target) && target.toUpperCase().contains(keyword.toUpperCase())) {
                return true;
            }
        }
        // 字节搜索
        // .... 暂时不需要
        return false;
    }

    //获取URL参数值
    public static String getUrlParam(String url, String name) {
        if (ValidateUtil.isNull(url) || ValidateUtil.isNull(name)) {
            return null;
        }
//        url.matches("(^|&)" + name + "=([^&]*)(&|$)");
//        Pattern p = Pattern.compile("(\\?|&+)(.+?)=([^&]*)");
        Pattern p = Pattern.compile("(\\?|&+)(" + name + ")=([^&]*)");
        Matcher m = p.matcher(url);
        while (m.find()) {
            return m.group(3);
//            System.out.println(m.group(2) + "=" + m.group(3));
        }
        return null;
    }

    // 去吃城市里的市
    public static String removeShi(String city) {
        if (ValidateUtil.isNull(city)) {
            return "";
        }
        if (city.lastIndexOf("市") == city.length() - 1) {
            return city.substring(0, city.length() - 1);
        }
        return city;
    }


    private static String baseRandString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";

    public static String getRandomString(int length) {        //length表示生成字符串的长度
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        int number = 0;
        for (int i = 0; i < length; i++) {
            number = random.nextInt(baseRandString.length());
            sb.append(baseRandString.charAt(number));
        }
        return sb.toString();
    }


    //正确的元转分方式
    public static Integer CorrectYuan2Fen(Double yuan) {
        //（重点）Double直接转BigDecimal丢失精度，此处需要将Double转换为String
        return new BigDecimal(String.valueOf(yuan)).movePointRight(2).intValue();
    }

    public static void main(String[] args) {
//        getUrlParam("http://jingyo.com?token=111&user=222", "token");
//        getUrlParam("http://jingyo.com?token=111&user=222", "user");


        System.out.println(removeShi("济南"));
    }
}
